﻿using System;
using System.Windows;
using TimeTableEasy.src.Database;
using TimeTableEasy.src.Core;


namespace TimeTableEasy
{
	public partial class LoginWindow
	{

		public LoginWindow()
		{
    		this.InitializeComponent();
		}

        private void LoginBtn_Click(object sender, RoutedEventArgs e)
        {
            String error = null;
            new OracleConnector();
            new Lang(out error);

            if (error != null)
            {
                label1.Content = error;
                return;
            }
            if (LoginLogin.Text.Length == 0 || LoginPassword.Password.Length == 0)
            {
                label1.Content = Lang.GetMessage(OpCodes.TimeTableEasyStrings.LANG_ALL_FIELD_NULL);
                return;
            }

            uint temp;
            if (!UInt32.TryParse(LoginLogin.Text, out temp))
            {
                label1.Content = Lang.GetMessage(OpCodes.TimeTableEasyStrings.LANG_ACCOUNT_NOT_FOUND);
                return;
            }

            if (Account.IsAccount(UInt32.Parse(LoginLogin.Text)))
            {
                if (Account.CheckPassword(UInt32.Parse(LoginLogin.Text), LoginPassword.Password.ToString()))
                {
                    new Window1(UInt32.Parse(LoginLogin.Text)).Show();
                    this.Close();
                }
                else
                    label1.Content = Lang.GetMessage(OpCodes.TimeTableEasyStrings.LANG_ACCOUNT_WRONG_USER_OR_PASS);                  
            }
            else
                label1.Content = Lang.GetMessage(OpCodes.TimeTableEasyStrings.LANG_ACCOUNT_NOT_FOUND);            
        }
	}
}